<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {

    public function index(){
		if(session('passid')){
			$this->success('您已登录', U('Index/index'));
		}else{
			$this->display('adminlte/login');
		}
    }
	
	public function login(){
		if(IS_POST){
			$TH =M("Teacher");
			$data['passid'] = I('post.userid');
			$result = $TH->where($data)->find();
			if(!$result){
				$this->error("用户名不存在");
			}
			$password = md5(I('post.password'));
			if($password != $result['password']){
				$this->error("密码错误");
			}
			if($result['state']==2){
				$this->error("您的账户被禁用,请联系管理员");
			}

			//存储SESSION
			session('id',$result['id']);        //用户ID
			session('passid',$result['passid']);    //用户账号
			session('sfid',$result['sfid']);      //用户身份
			session('name',$result['name']);      //用户名称
			session('logindate',$result['logindate']); //上一次登录时间

			//更新登录时间
			$TH->where($data)->setField('logindate',date("Y-m-d H-i-s" ,time()));
			
			//存储COOKIE
			cookie('uid',$result['id']);
			cookie('userid',$result['passid']);
			cookie('sfid',$result['sfid']);
			cookie('name',$result['name']);
			cookie('telephone',$result['telephone']);
			cookie('logindate',$result['logindate']);
			cookie('notes',$result['notes']);

			$this->redirect('index/index');
			
		}else{
			$this->error("非法请求");
		}
	}


	//注销登录
	public function logout(){
		session(null);
		cookie(null);
		$this->redirect('login/index');
	}
	
	
	
	
	

	


	



}